package com.edie.customview.modular;

import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.edie.customview.R;
import com.edie.customview.data.entity.DBTestEntity;
import com.edie.customview.gen.DBTestEntityDao;
import com.edie.customview.gen.GreenDaoManager;

import java.util.List;

public class DatabaseActivity extends AppCompatActivity implements View.OnClickListener {

    private DBTestEntityDao mDbTestEntityDao;
    //控件
    private TextView dataResultTv;
    private EditText conditionEt;

    public static void start(Context context) {
        context.startActivity(new Intent(context, DatabaseActivity.class));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_database);
        initView();
        mDbTestEntityDao = GreenDaoManager.getInstance().getDaoSession().getDBTestEntityDao();
    }

    private void initView() {
        conditionEt = (EditText) findViewById(R.id.condition_et);
        dataResultTv = (TextView) findViewById(R.id.data_result_tv);

        findViewById(R.id.insert_btn).setOnClickListener(this);
        findViewById(R.id.delete_by_id_btn).setOnClickListener(this);
        findViewById(R.id.delete_by_name_btn).setOnClickListener(this);
        findViewById(R.id.delete_btn).setOnClickListener(this);
        findViewById(R.id.update_btn).setOnClickListener(this);
        findViewById(R.id.search_btn).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        String condition = getCondition();
        switch (v.getId()) {
            case R.id.insert_btn:
                if (!TextUtils.isEmpty(condition)) {
                    Long i = Long.valueOf(condition);
                    DBTestEntity entity = new DBTestEntity(i, "呵呵" + i);
                    mDbTestEntityDao.insert(entity);
                }
                break;
            case R.id.delete_by_id_btn:
                mDbTestEntityDao.deleteByKey(Long.valueOf(condition));
                break;
            case R.id.delete_by_name_btn:
                Long aLong = Long.valueOf(condition);
                DBTestEntity entity1 = new DBTestEntity(aLong, "呵呵" + aLong);
                mDbTestEntityDao.delete(entity1);
                break;
            case R.id.delete_btn:
                mDbTestEntityDao.deleteAll();
                break;
            case R.id.update_btn:
                Long aaLong = Long.valueOf(condition);
                DBTestEntity entityAA = new DBTestEntity(aaLong, "哈哈哈" + aaLong);
                mDbTestEntityDao.update(entityAA);
                break;
            case R.id.search_btn:
                List<DBTestEntity> dbTestEntities = mDbTestEntityDao.loadAll();
                dataResultTv.setText(dbTestEntities.toString());
                break;
        }
    }

    public String getCondition() {
        return conditionEt.getText().toString().trim();
    }

    public static class DBOpenHelper extends SQLiteOpenHelper {
        private DBOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
            super(context, name, factory, version);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        }
    }
}
